package com.tanhua.server.service;

import com.alibaba.fastjson.JSON;
import com.tanhua.dubbo.api.*;
import com.tanhua.model.domain.*;
import com.tanhua.model.dto.SoulReportDto;
import com.tanhua.model.dto.Answers;
import com.tanhua.model.enums.Level;
import com.tanhua.model.vo.*;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class TestSoulService {
    @DubboReference
    private ExamApi examApi;

    @DubboReference
    private QuestionsApi questionsApi;

    @DubboReference
    private OptionsApi optionsApi;

    @DubboReference
    private ReportApi reportApi;

    @DubboReference
    private ConclusionApi conclusionApi;
    @DubboReference
    private UserInfoApi userInfoApi;

    public List<ExamVo> getExam() {
        Report byUserId = reportApi.findByUserId(UserHolder.getUserId());

        List<Exam> exams = examApi.findAll();
        List<ExamVo> examVos = new ArrayList<>();
        for (Exam exam : exams) {
            List<Questions> questions = questionsApi.findByExamId(exam.getId());
            List<QuestionsVo> questionsVos = new ArrayList<>();
            for (Questions question : questions) {
                QuestionsVo questionsVo = new QuestionsVo();
                BeanUtils.copyProperties(question, questionsVo);
                questionsVo.setId(String.valueOf(question));
                List<Options> options = optionsApi.findByQuestionsId(Integer.valueOf(question.getId()) );
                questionsVo.setOptions(options);
                questionsVos.add(questionsVo);
            }
            ExamVo examVo = new ExamVo();
            BeanUtils.copyProperties(exam, examVo);
            examVo.setId(String.valueOf(exam.getId()));
            examVo.setLevel(Level.getname(exam.getLevel()));
            examVo.setQuestions(questionsVos);
            examVo.setIsLock(1);
            examVos.add(examVo);
        }
        examVos.get(0).setIsLock(0);
        if(byUserId!=null){
            examVos.get(1).setIsLock(0);
            examVos.get(0).setReportId(String.valueOf(byUserId.getConclusionId()));

            if(byUserId.getLevel() > 1){
                examVos.get(1).setReportId(String.valueOf(byUserId.getConclusionId()));

                examVos.get(2).setIsLock(0);
            }
            if(byUserId.getLevel() ==3){
                examVos.get(2).setReportId(String.valueOf(byUserId.getConclusionId()));

            }
        }
        System.out.println(examVos);
        return examVos;
    }


    public String report(Map<String, List<SoulReportDto>> answers) {

        List<SoulReportDto> SoulReportDtos = answers.get("answers");
        String questionId =optionsApi.findQuestionIdById(SoulReportDtos.get(0).getOptionId());
        int level =questionsApi.findLevelById(questionId);
        int score = 0;
        for (SoulReportDto soulReportDto : SoulReportDtos) {
            score += Integer.valueOf(soulReportDto.getOptionId());
        }
        Conclusion conclusion = conclusionApi.findByScore(score);
        Long id = conclusion.getId();

        Report byUserId = reportApi.findByUserId(UserHolder.getUserId());
        if(byUserId == null){
            Report report = new Report();
            report.setLevel(level);
            report.setUserId(UserHolder.getUserId());
            if(id==null || id == 0){id=1L;}
            report.setConclusionId(id);
            reportApi.save(report);
        }else {
            byUserId.setLevel(level);
            byUserId.setConclusionId(id);
            reportApi.updateById(byUserId);
        }


        return String.valueOf(id);
    }


    public ConclusionVo getReport(int id) {
        Conclusion conclusion = conclusionApi.getById(id);
        List<Dimensions> list = (List) JSON.parse(conclusion.getDimensions());
        ConclusionVo conclusionVo = new ConclusionVo();
        BeanUtils.copyProperties(conclusion,conclusionVo);
        conclusionVo.setDimensions(list);

        //根据报告id获取相同的userId
        List<Long> userId =  reportApi.getUserIdByConclusionId(id);
        ArrayList<SimilarYou> similarYous = new ArrayList<>();

        if(userId!=null){
            Map<Long, UserInfo> byIds = userInfoApi.findByIds(userId, null);

            for (Long aLong : userId) {
                if(aLong.equals(UserHolder.getUserId())){
                    continue;
                }
                SimilarYou similarYou = new SimilarYou();
                similarYou.setId(aLong);
                similarYou.setAvatar(byIds.get(aLong).getAvatar());
                similarYous.add(similarYou);
            }
        }
        conclusionVo.setSimilarYou(similarYous);
        System.out.println(conclusionVo+"=============================================================");
        return conclusionVo;
    }
}
